package com.xbongbong.saas.script.tempenum;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.i18n.util.I18nMessageUtil;
import com.xbongbong.paas.enums.DateTypeEnum;
import com.xbongbong.paas.enums.DefaultTypeEnum;
import com.xbongbong.paas.enums.DefaultValueEnum;
import com.xbongbong.paas.help.FormDataFormExplainHelper;
import com.xbongbong.paas.field.DefaultAttrPoJo;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.field.FormulaPoJo;
import com.xbongbong.paas.field.ItemPoJo;
import com.xbongbong.paas.field.LinkDetailPojo;
import com.xbongbong.paas.field.NumericalLimitsPoJo;
import com.xbongbong.pro.enums.AmountFlagEnum;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.enums.SummaryTypeEnum;
import com.xbongbong.paas.field.SaasParticularAttributePoJo;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.constant.StringConstant;
import com.xbongbong.saas.enums.NumberTypeEnum;
import com.xbongbong.saas.enums.SelectProductEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.enums.business.QuotationEnum;
import com.xbongbong.saas.enums.dictionary.ArchivedEnum;
import com.xbongbong.saas.enums.dictionary.QuotationStatusEnum;
import com.xbongbong.saas.script.help.InitExplainsHelper;
import com.xbongbong.sys.enums.RoleEnum;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import lombok.Getter;

/**
 * @author long.rao
 * @version v1.0
 * @date 2019/7/25 10:40
 * @since v1.0
 */
@Getter
public enum TempQuotationEnum {
    BASE_INFO(QuotationEnum.BASE_INFO),
    QUOTATION_NO(QuotationEnum.QUOTATION_NO),
    NAME(QuotationEnum.NAME),
    CUSTOMER_ID(QuotationEnum.CUSTOMER_ID),
    CONTACT_ID(QuotationEnum.CONTACT_ID),
    OPPORTUNITY_ID(QuotationEnum.OPPORTUNITY_ID),
    QUOTATION_PERSON(QuotationEnum.QUOTATION_PERSON),
    QUOTATION_TIME(QuotationEnum.QUOTATION_TIME),
    EFFECTIVE_TIME(QuotationEnum.EFFECTIVE_TIME),
    STATUS(QuotationEnum.STATUS),
    OWNER_ID(QuotationEnum.OWNER_ID),
    MINOR_USER(QuotationEnum.MINOR_USER),

    SALE_INFO(QuotationEnum.SALE_INFO),
    PRODUCT(QuotationEnum.PRODUCT),
    PRODUCT_TOTAL(QuotationEnum.PRODUCT_TOTAL),
    DISCOUNT(QuotationEnum.DISCOUNT),
    OTHER_EXPENSE(QuotationEnum.OTHER_EXPENSE),
    DISCOUNT_AMOUNT(QuotationEnum.DISCOUNT_AMOUNT),
    AMOUNT(QuotationEnum.AMOUNT),
    ESTIMATE_COST(QuotationEnum.ESTIMATE_COST),
    GROSS_PROFIT(QuotationEnum.GROSS_PROFIT),
    GROSS_PROFIT_RATE(QuotationEnum.GROSS_PROFIT_RATE),

    EXTEND_INFO(QuotationEnum.EXTEND_INFO),

    SYSTEM_INFO(QuotationEnum.SYSTEM_INFO),
    CREATOR_ID(QuotationEnum.CREATOR_ID),
    ADD_TIME(QuotationEnum.ADD_TIME),
    UPDATE_TIME(QuotationEnum.UPDATE_TIME),

    VERSION(QuotationEnum.VERSION),
    ARCHIVED(QuotationEnum.ARCHIVED),
    CUSTOMER_NAME(QuotationEnum.CUSTOMER_NAME),
    CONTACT_NAME(QuotationEnum.CONTACT_NAME),
    OPPORTUNITY_NAME(QuotationEnum.OPPORTUNITY_NAME),
    QUOTATION_NAME(QuotationEnum.QUOTATION_NAME),
    GROUP_NUMBER(QuotationEnum.GROUP_NUMBER),
    IS_NEW(QuotationEnum.IS_NEW)
    ;

    /**
     * 报价单枚举
     */
    private QuotationEnum anEnum;

    TempQuotationEnum(QuotationEnum anEnum) {
        this.anEnum = anEnum;
    }

    public static List<TempQuotationEnum> getAllEnum() {
        List<TempQuotationEnum> list = new ArrayList<>();
        Collections.addAll(list, values());
        return list;
    }

    /**
     * 报价编号
     * @param fieldAttrEntity
     */
    public static void initQuotationNo(FieldAttrEntity fieldAttrEntity) {
        LinkDetailPojo linkDetailPojo = new LinkDetailPojo();
        linkDetailPojo.setBusinessType(XbbRefTypeEnum.QUOTATION.getCode());
        linkDetailPojo.setSubBuisenssType(XbbRefTypeEnum.QUOTATION.getCode());
        linkDetailPojo.setSaasMark(SaasMarkEnum.SAAS.getCode());
        linkDetailPojo.setAttr(QUOTATION_NO.getAnEnum().getAttr());
        fieldAttrEntity.setLinkDetail(linkDetailPojo);
        fieldAttrEntity.setNoRepeat(1);
        fieldAttrEntity.setSerialNumber(InitExplainsHelper.setSerialNumber(SerialNumberEnum.QUOTATION_NO));
        fieldAttrEntity.setIsForbidden(0);
    }

    /**
     * 报价名称
     * @param fieldAttrEntity
     */
    public static void initName(FieldAttrEntity fieldAttrEntity) {
        LinkDetailPojo linkDetailPojo = new LinkDetailPojo();
        linkDetailPojo.setBusinessType(XbbRefTypeEnum.QUOTATION.getCode());
        linkDetailPojo.setSubBuisenssType(XbbRefTypeEnum.QUOTATION.getCode());
        linkDetailPojo.setSaasMark(SaasMarkEnum.SAAS.getCode());
        linkDetailPojo.setAttr(NAME.getAnEnum().getAttr());
        fieldAttrEntity.setLinkDetail(linkDetailPojo);
        fieldAttrEntity.setNoRepeat(1);
    }

    /**
     * 关联客户
     * @param fieldAttrEntity
     */
    public static void initCustomerId(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setLinkedType(XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode());
        fieldAttrEntity.setLinkedText(CUSTOMER_NAME.getAnEnum().getAttr());
        SaasParticularAttributePoJo saasParticularAttributePoJo = new SaasParticularAttributePoJo();
        saasParticularAttributePoJo.setMultiCheck(false);
        saasParticularAttributePoJo.setRelyMode("customer");
        saasParticularAttributePoJo.setRelyType("base");
        fieldAttrEntity.setSaasParticularAttributePoJo(saasParticularAttributePoJo);
        fieldAttrEntity.setIsForbidden(0);
    }

    /**
     * 关联联系人
     * @param fieldAttrEntity
     */
    public static void initContactId(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setLinkedType(XbbRefTypeEnum.CONTACT.getCode());
        fieldAttrEntity.setLinkedText(CONTACT_NAME.getAnEnum().getAttr());
        SaasParticularAttributePoJo saasParticularAttributePoJo = new SaasParticularAttributePoJo();
        saasParticularAttributePoJo.setMultiCheck(false);
        saasParticularAttributePoJo.setRelyMode("customer");
        saasParticularAttributePoJo.setRelyType("relied");
        saasParticularAttributePoJo.setRelyBusiness(XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode());
        fieldAttrEntity.setSaasParticularAttributePoJo(saasParticularAttributePoJo);
    }

    /**
     * 关联机会
     * @param fieldAttrEntity
     */
    public static void initOpportunityId(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setLinkedType(XbbRefTypeEnum.SALES_OPPORTUNITY.getCode());
        fieldAttrEntity.setLinkedText(OPPORTUNITY_NAME.getAnEnum().getAttr());
        SaasParticularAttributePoJo saasParticularAttributePoJo = new SaasParticularAttributePoJo();
        saasParticularAttributePoJo.setMultiCheck(false);
        saasParticularAttributePoJo.setRelyMode("customer");
        saasParticularAttributePoJo.setRelyType("relied");
        saasParticularAttributePoJo.setRelyBusiness(XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode());
        fieldAttrEntity.setSaasParticularAttributePoJo(saasParticularAttributePoJo);
    }

    /**
     * 报价人
     * @param fieldAttrEntity
     */
    public static void initQuotationPerson(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setLinkedText(QUOTATION_NAME.getAnEnum().getAttr());
        fieldAttrEntity.setIsForbidden(0);
        DefaultAttrPoJo defaultAttrPoJo = new DefaultAttrPoJo();
        defaultAttrPoJo.setDefaultType(DefaultTypeEnum.DYNAMIC.getAlias());
        defaultAttrPoJo.setDefaultValue(DefaultValueEnum.LOGIN_USER.getValue());
        defaultAttrPoJo.setDefaultList(new ArrayList<>());
        fieldAttrEntity.setDefaultAttr(defaultAttrPoJo);
    }

    /**
     * 报价日期
     * @param fieldAttrEntity
     */
    public static void initQuotationTime(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setDateType(DateTypeEnum.YMD.getAlias());
        fieldAttrEntity.setIsForbidden(0);
        DefaultAttrPoJo defaultAttrPoJo = new DefaultAttrPoJo();
        defaultAttrPoJo.setDefaultType(DefaultTypeEnum.TODAY.getAlias());
        fieldAttrEntity.setDefaultAttr(defaultAttrPoJo);
    }

    /**
     * 有效日期
     * @param fieldAttrEntity
     */
    public static void initEffectiveTime(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setDateType(DateTypeEnum.YMD.getAlias());
    }

    /**
     * 报价状态
     * @param fieldAttrEntity
     */
    public static void initStatus(FieldAttrEntity fieldAttrEntity) {
        List<ItemPoJo> itemPoJos = InitExplainsHelper.setCombItemByEnum(QuotationStatusEnum.class, fieldAttrEntity.getFieldType());
        itemPoJos.forEach(itemPoJo -> {
            if (Objects.equals(itemPoJo.getValue().toString(), QuotationStatusEnum.CREATED.getCode())) {
                itemPoJo.setValue(itemPoJo.getValue().toString());
                itemPoJo.setNoMove(BasicConstant.ONE);
                itemPoJo.setNoEdit(BasicConstant.ONE);
                itemPoJo.setNoDel(BasicConstant.ONE);
            } else if (Objects.equals(itemPoJo.getValue().toString(), QuotationStatusEnum.IN_QUOTATION.getCode())) {
                itemPoJo.setValue(itemPoJo.getValue().toString());
                itemPoJo.setChecked(true);
            } else if (Objects.equals(itemPoJo.getValue().toString(), QuotationStatusEnum.INVALID.getCode())) {
                itemPoJo.setValue(itemPoJo.getValue().toString());
            }
        });
        fieldAttrEntity.setItems(itemPoJos);
        fieldAttrEntity.setIsForbidden(0);
        fieldAttrEntity.setDictionaryFlag(BasicConstant.ONE);
    }

    /**
     * 协同人
     * @param fieldAttrEntity
     */
    public static void initCoUserId(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setRequired(0);
        fieldAttrEntity.setShowEditable(0);
        fieldAttrEntity.setAdd(1);
        fieldAttrEntity.setAddAdvanceEnable(1);
        fieldAttrEntity.setShowAdd(1);
        fieldAttrEntity.setRemove(1);
        fieldAttrEntity.setRemoveAdvanceEnable(1);
        fieldAttrEntity.setShowRemove(1);
        fieldAttrEntity.setIsForbidden(0);
        fieldAttrEntity.setEditable(1);
        fieldAttrEntity.setUnableEditMemo(null);
        fieldAttrEntity.setAddRule(FormDataFormExplainHelper.getDefaultRule(RoleEnum.MANAGER));
        fieldAttrEntity.setRemoveRule(FormDataFormExplainHelper.getDefaultRule(RoleEnum.MANAGER));
    }

    /**
     * 负责人
     * @param fieldAttrEntity
     */
    public static void initOwnerId(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setRequired(1);
        //可新增权限：默认【超级管理员、销售主管、创建人、负责人、负责人主管】可新增负责人，支持自定义；
        fieldAttrEntity.setShowEditable(0);
        fieldAttrEntity.setAdd(1);
        fieldAttrEntity.setAddAdvanceEnable(1);
        fieldAttrEntity.setShowAdd(1);
        //可删除权限：默认【超级管理员、销售主管】可删除负责人，支持自定义；
        fieldAttrEntity.setRemove(1);
        fieldAttrEntity.setRemoveAdvanceEnable(1);
        fieldAttrEntity.setShowRemove(1);
        fieldAttrEntity.setMultiple(1);
        fieldAttrEntity.setIsForbidden(0);
        fieldAttrEntity.setEditable(1);
        fieldAttrEntity.setUnableEditMemo(null);
        fieldAttrEntity.setAddRule(FormDataFormExplainHelper.getDefaultRule(RoleEnum.MANAGER));
        fieldAttrEntity.setRemoveRule(FormDataFormExplainHelper.getDefaultRule(RoleEnum.MANAGER));
    }

    public static void initProductTotal(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setAccuracy(2);
        fieldAttrEntity.setMemo("默认值为产品报价总计");
        fieldAttrEntity.setSummaryFlag(SummaryTypeEnum.SUM.getType());
        fieldAttrEntity.setAmountFlag(AmountFlagEnum.YES.getType());
        DefaultAttrPoJo defaultAttrPoJo = new DefaultAttrPoJo();
        FormulaPoJo formulaInfo = new FormulaPoJo();
        String formula = "SUM({self." + QuotationEnum.PRODUCT.getAttr() + "." + SelectProductEnum.SUBTOTAL.getAttr() + "})";
        JSONObject labelMap = new JSONObject();
        labelMap.put("self." + QuotationEnum.PRODUCT.getAttr() + "." + SelectProductEnum.SUBTOTAL.getAttr(), "关联产品.单价小计");
        formulaInfo.setFormula(formula);
        formulaInfo.setLabelMap(labelMap);
        defaultAttrPoJo.setDefaultType("formula");
        defaultAttrPoJo.setFormulaInfo(formulaInfo);
        fieldAttrEntity.setDefaultAttr(defaultAttrPoJo);
        // 合同金额默认限定 0 ~ 100000000000
        NumericalLimitsPoJo numericalLimits = new NumericalLimitsPoJo();
        numericalLimits.setMin(0D);
        numericalLimits.setMax(100000000000D);
        fieldAttrEntity.setNumericalLimits(numericalLimits);
        fieldAttrEntity.setNumericalLimitsFlag(1);
    }

    /**
     * 整单折扣默认为百分比，所以默认值为1（新建编辑时候就会默认展示为100%）
     * @param fieldAttrEntity
     */
    public static void initDiscount(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setAccuracy(2);
        fieldAttrEntity.setAmountFlag(AmountFlagEnum.NO.getType());
        fieldAttrEntity.setIntegerOnly(BasicConstant.ZERO);
        fieldAttrEntity.setNumberType(NumberTypeEnum.RATE.getCode());
        NumericalLimitsPoJo numericalLimits = new NumericalLimitsPoJo();
        numericalLimits.setMax(100D);
        numericalLimits.setMin(1D);
        fieldAttrEntity.setNumericalLimits(numericalLimits);
        fieldAttrEntity.setNumericalLimitsFlag(1);
        DefaultAttrPoJo defaultAttrPoJo = new DefaultAttrPoJo();
        defaultAttrPoJo.setDefaultType(DefaultTypeEnum.CUSTOM.getAlias());
        defaultAttrPoJo.setDefaultValue(BasicConstant.ONE);
        fieldAttrEntity.setDefaultAttr(defaultAttrPoJo);
    }

    public static void initOtherExpense(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setAccuracy(2);
        fieldAttrEntity.setSummaryFlag(SummaryTypeEnum.SUM.getType());
        fieldAttrEntity.setAmountFlag(AmountFlagEnum.YES.getType());
    }

    public static void initDiscountAmount(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setAccuracy(2);
        fieldAttrEntity.setSummaryFlag(SummaryTypeEnum.SUM.getType());
        fieldAttrEntity.setAmountFlag(AmountFlagEnum.YES.getType());
    }


    public static void initAmount(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setSummaryFlag(SummaryTypeEnum.SUM.getType());
        fieldAttrEntity.setAmountFlag(AmountFlagEnum.YES.getType());
        fieldAttrEntity.setMemo("报价金额=产品合计*整单折扣率+其他费用-优惠金额");
        DefaultAttrPoJo defaultAttrPoJo = new DefaultAttrPoJo();
        FormulaPoJo formulaInfo = new FormulaPoJo();
        String formula = "SUM({self." + QuotationEnum.PRODUCT_TOTAL.getAttr() + "}*IF(OR(ISEMPTY({self." + QuotationEnum.DISCOUNT.getAttr() + "}),{self." + QuotationEnum.DISCOUNT.getAttr() + "}==0),1,{self." + QuotationEnum.DISCOUNT.getAttr() + "}),{self." + QuotationEnum.OTHER_EXPENSE.getAttr() + "})-{self." + QuotationEnum.DISCOUNT_AMOUNT.getAttr() + "}";
        JSONObject labelMap = new JSONObject();
        labelMap.put("self." + QuotationEnum.PRODUCT_TOTAL.getAttr(), QuotationEnum.PRODUCT_TOTAL.getAttrName());
        labelMap.put("self." + QuotationEnum.DISCOUNT.getAttr(), QuotationEnum.DISCOUNT.getAttrName());
        labelMap.put("self." + QuotationEnum.OTHER_EXPENSE.getAttr(), QuotationEnum.OTHER_EXPENSE.getAttrName());
        labelMap.put("self." + QuotationEnum.DISCOUNT_AMOUNT.getAttr(), QuotationEnum.DISCOUNT_AMOUNT.getAttrName());
        formulaInfo.setFormula(formula);
        formulaInfo.setLabelMap(labelMap);
        defaultAttrPoJo.setDefaultType("formula");
        defaultAttrPoJo.setFormulaInfo(formulaInfo);
        fieldAttrEntity.setDefaultAttr(defaultAttrPoJo);
        fieldAttrEntity.setIsForbidden(0);
    }



    public static void initEstimateCost(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setSummaryFlag(SummaryTypeEnum.SUM.getType());
        fieldAttrEntity.setAmountFlag(AmountFlagEnum.YES.getType());
        fieldAttrEntity.setMemo("预估成本 = 报价单产品总成本 + 其他费用");
        fieldAttrEntity.setUnableEditMemo(I18nMessageUtil.getMessage(StringConstant.AUTO_GEN_FIELD));
        fieldAttrEntity.setEditable(0);
    }

    public static void initGrossProfit(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setSummaryFlag(SummaryTypeEnum.SUM.getType());
        fieldAttrEntity.setAmountFlag(AmountFlagEnum.YES.getType());
        fieldAttrEntity.setMemo("预估毛利=报价金额-报价产品总成本-其它费用");
        fieldAttrEntity.setUnableEditMemo(I18nMessageUtil.getMessage(StringConstant.AUTO_GEN_FIELD));
        fieldAttrEntity.setEditable(0);
    }

    public static void initGrossProfitRate(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setSummaryFlag(SummaryTypeEnum.AVERAGE.getType());
        fieldAttrEntity.setMemo("预估毛利率 = 预估毛利/报价金额*100%");
        fieldAttrEntity.setAccuracy(2);
        fieldAttrEntity.setUnableEditMemo(I18nMessageUtil.getMessage(StringConstant.AUTO_GEN_FIELD));
        fieldAttrEntity.setEditable(0);
    }

    public static void initArchived(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setItems(InitExplainsHelper.setCombItemByEnum(ArchivedEnum.class, fieldAttrEntity.getFieldType()));
    }

    public static void initCreatorId(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setUnableEditMemo(I18nMessageUtil.getMessage(StringConstant.AUTO_GEN_FIELD));
    }
    public static void initAddTime(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setUnableEditMemo(I18nMessageUtil.getMessage(StringConstant.AUTO_GEN_FIELD));
    }

    public static void initUpdateTime(FieldAttrEntity fieldAttrEntity) {
        fieldAttrEntity.setEditable(0);
        fieldAttrEntity.setUnableEditMemo(I18nMessageUtil.getMessage(StringConstant.AUTO_GEN_FIELD));
    }

    public static void initIsNew(FieldAttrEntity fieldAttrEntity){
        List<ItemPoJo> itemPoJos = new ArrayList<>();
        ItemPoJo itemPoJoTrue = new ItemPoJo();
        itemPoJoTrue.setText("是");
        itemPoJoTrue.setValue(1);
        itemPoJoTrue.setChecked(false);
        ItemPoJo itemPoJoFalse = new ItemPoJo();
        itemPoJoFalse.setText("否");
        itemPoJoFalse.setValue(0);
        itemPoJoFalse.setChecked(true);
        itemPoJos.add(itemPoJoFalse);
        itemPoJos.add(itemPoJoTrue);
        fieldAttrEntity.setItems(itemPoJos);
    }

    public static void initProducts(FieldAttrEntity fieldAttrEntity){
        fieldAttrEntity.setIsForbidden(0);
    }
}
